
#ifndef APP_OTA_SERVER_H_
#define APP_OTA_SERVER_H_

#include "common.h"
#include "pin.h"
#include "sys_console.h"
#include "app_cfg.h"
#include "app_spi.h"
#include "app_init.h"
#include "app_factory.h"


#ifndef BLE_OTA_FW_CHECK_EN
#define BLE_OTA_FW_CHECK_EN					1
#endif
		

extern  uint8_t ota_first_flag;
//extern const uint8_t version_vel[8];
extern  uint8_t version_vel[8];
extern uint8_t version_vel_size;
extern const uint8_t factory_version_vel[10];
//extern uint8_t device_version[2];
extern uint8_t device_version[4];
extern uint8_t sn_val[16];
extern  const uint8_t otaPackageName[17];
#define CMD_OTA_FW_VERSION					0xff00



#define CMD_OTA_START_EXT					0xFF03	//client -> server
#define CMD_OTA_FW_VERSION_REQ				0xFF04
#define CMD_OTA_FW_VERSION_RSP				0xFF05
#define CMD_OTA_RESULT						0xFF06	//server -> client

#define SERVICE_OTA_STATE					0x2d03

#define HN_VERSION_OTA_STATE				0x0107


#define HN_CMD_OTA_START					0x0901
#define HN_CMD_OTA_PARAM					0x0902
#define HN_CMD_OTA_DATA						0x0903

#define HN_CMD_OTA_FILE_CONTENT				0x0904
#define HN_CMD_OTA_SIZE						0x0905

#define HN_CMD_OTA_CHECK					0x0906
#define HN_CMD_OTA_STATE					0x0907

#define HN_CMD_OTA_CANCEL					0x0908
#define HN_CMD_OTA_STATUS					0x0909


#define FLAG_FW_CHECK						0x5D
#define FW_CHECK_AGTHM2						0x02

#define 		FW_MIN_SIZE											0x04000	 //16K
#define 		FW_MAX_SIZE											0x32000	 //256K(0x40000) - 56K = 200K

#define 		OTA_FLOW_VERSION									BIT(0)
#define 		OTA_FLOW_START										BIT(1)
#define 		OTA_FLOW_DATA_COME									BIT(2)
#define 		OTA_FLOW_VALID_DATA									BIT(3)
#define 		OTA_FLOW_GET_SIZE									BIT(5)
#define 		OTA_FLOW_END										BIT(6)


#define 		OTA_STEP_IDLE										0
#define 		OTA_STEP_START										BIT(0)
#define 		OTA_STEP_DATA										BIT(1)
#define 		OTA_STEP_FEEDBACK									BIT(2)  //feedback OTA result to peer device
#define 		OTA_STEP_FINISH										BIT(3)

#define 		DATA_PENDING_VERSION_RSP							1
#define 		DATA_PENDING_OTA_RESULT								2
#define 		DATA_PENDING_TERMINATE_CMD							3

typedef struct{
	uint8_t  ota_start_flag;
#if (BLE_OTA_FW_CHECK_EN)
	uint8_t 	fw_check_en;
	uint8_t  fw_check_match;
	uint8_t  rsvd;

	uint32_t fw_crc_init;

	uint16_t fw_crc_last_index;
#endif
}ota_service_t;

extern ota_service_t blcOta;


extern uint8_t ota_state_flag;

extern uint32_t	ota_program_offset;
extern int 	ota_firmware_size_k;

/**
 * @brief	OTA result
 */
enum{
	//0x00
	OTA_SUCCESS 			= 0,			//success
	OTA_DATA_PACKET_SEQ_ERR,				//OTA data packet sequence number error: repeated OTA PDU or lost some OTA PDU
	OTA_PACKET_INVALID,						//invalid OTA packet: 1. invalid OTA command; 2. addr_index out of range; 3.not standard OTA PDU length
	OTA_DATA_CRC_ERR,						//packet PDU CRC err

	//0x04
	OTA_WRITE_FLASH_ERR,					//write OTA data to flash ERR
 	OTA_DATA_UNCOMPLETE,					//lost last one or more OTA PDU
 	OTA_FLOW_ERR,		    				//peer device send OTA command or OTA data not in correct flow
 	OTA_FW_CHECK_ERR,						//firmware CRC check error

	//0x08
	OTA_VERSION_COMPARE_ERR,				//the version number to be update is lower than the current version
	OTA_PDU_LEN_ERR,						//PDU length error: not 16*n, or not equal to the value it declare in "CMD_OTA_START_EXT" packet
	OTA_FIRMWARE_MARK_ERR,		    		//firmware mark error: not generated by telink's BLE SDK
	OTA_FW_SIZE_ERR,						//firmware size error: no firmware_size; firmware size too small or too big

	//0x0C
	OTA_DATA_PACKET_TIMEOUT,	   			//time interval between two consequent packet exceed a value(user can adjust this value)
 	OTA_TIMEOUT,							//OTA flow total timeout
 	OTA_FAIL_DUE_TO_CONNECTION_TERMIANTE,	//OTA fail due to current connection terminate(maybe connection timeout or local/peer device terminate connection)
 	OTA_LOGIC_ERR,							//software logic error, please contact FAE of TeLink
};

typedef struct{
	uint8_t	ota_step;
	uint8_t	otaResult;
	uint8_t	version_accept;
	uint8_t	resume_mode;   //1: resume_mode enable; 0: resume_mode disable

	uint8_t	ota_busy;
	uint8_t 	fw_check_en;
	uint8_t  fw_check_match;
	uint8_t	flow_mask;

	uint8_t	pdu_len; 	    //OTA valid data length
	uint8_t	last_pdu_crc_offset;
	uint8_t	last_actual_pdu_len;
	uint8_t	last_valid_pdu_len;  //maximum value 240

	uint8_t	data_pending_type;  //mark, and also data length
	uint8_t	u8_rsvd[3];

	uint16_t local_version_num;  //default value:0; use use API to set version
	uint16_t ota_attHandle;
	uint16_t ota_connHandle;
	uint16_t last_adr_index;

	uint32_t fw_crc_default;
	uint32_t fw_crc_init;
	uint32_t fw_crc_addr;

	uint32_t firmware_size;
	int flash_addr_mark;  //must be "s32", have special useage with "< 0"
	int cur_adr_index; //must be "s32"

	uint32_t feedback_begin_tick;  //add a OTA feedback timeout control, prevent some extreme case which lead to OTA flow blocked
	uint32_t ota_start_tick;
	uint32_t data_packet_tick;
	uint32_t process_timeout_us;
	uint32_t packet_timeout_us;

}ota_server_t_a;

extern ota_server_t_a blotaSvr_a;

typedef struct{
	uint32_t ota_offset_a;
	uint32_t ota_length_a;
	//uint8_t file_apply_bitmap[];
	uint16_t ota_unit_size_a;
	uint16_t app_wait_Timeout_s_a;
	uint16_t device_restart_Timeout_s_a;
	uint32_t error_code;
	uint8_t battery_threshold;
	uint16_t interval;
	uint8_t ack_enable;
	uint8_t APP_OTA_Status;
	uint32_t package_valid_size;
	uint32_t received_file_size;

	uint32_t otabin_file_size;
	uint8_t device_version_size;
	uint8_t sn_size;
	uint32_t ota_offset_slaver;
	uint32_t ota_length_tset;
}ota_server_honor;
extern ota_server_honor blhonor_otaSvr;



extern void otaWrite_honor( void * p, uint16_t len);


extern void ota_data_init(void);


extern void app_ota_timer_tick_0_1ms_handler(void);





#endif /* STACK_BLE_SERVICE_OTA_OTA_SERVER_H_ */
